(function () {
    
    var urlInfo = "/bulletin/content/info.do";
    var urlRead = "/bulletin/read.do";
    var dataUrl = "/bulletin/list.do";
    var iWidth = document.documentElement.offsetWidth;//获取屏幕宽度-------------------------------------------
    var iHeight = document.documentElement.offsetHeight;//获取屏幕高度----------------------------
    var Permission = true;
    var adopt = true;
    var limit=[];
    var searchValue;

    $(function () {
        $_setDatagrid()
    })





    function $_setDatagrid(pageSize,pageNum,search) {

        var permit = true;
        var getColumns = [
            { field: 'type', title: '类型', width: iWidth*0.02, align: 'center',
                formatter: function (value, row, index) {
                    if(value == 1){
                        var str = "通知";
                    }
                    else if(value == 2){
                        var str = "公告";
                    }

                    return str;
                }
            },
            { field: 'title', title: '名称', width: iWidth*0.3, align: 'left',halign: 'center' },
            { field: 'resources', title: '单位来源', width: iWidth*0.04, align: 'center' },
            { field: 'mtime', title: '日期', width: iWidth*0.04, align: 'center' ,

                formatter: function (value, row, index) {
                    var myDate = new Date(value);
                    var result=myDate.getFullYear()+"年"+(myDate.getMonth()+1)+"月"+myDate.getDate()+'日'
                    return result;
                }
            },
            { field: 'editor', title: '发布人', width:  iWidth*0.04, align: 'center' },
        ];
        var dataList;
        var start;

        if(!pageSize&&!pageNum){
            if(search){
                dataList = {
                    page:1,
                    pageSize:20,
                    title:search
                }
                start = 0;
            }
            else {
                dataList = {
                    page:1,
                    pageSize:20,
                }
                start = 0;
            }
        }else {
            // dataList = {
            //     page:pageNum,
            //     pageSize:pageSize,
            // }
            // start = (pageNum-1) * pageSize;

            //-------------------------------------------------------修改处
            if (search) {
                dataList = {
                    page: pageNum,
                    pageSize: pageSize,
                    title:search
                }
                start = (pageNum - 1) * pageSize;
            }
            else {
                dataList = {
                    page: pageNum,
                    pageSize: pageSize,
                }
                start = (pageNum - 1) * pageSize;
            }
            //-------------------------------------------------------
        }


        $.get(dataUrl,dataList,function (json) {

            var obj = eval('(' + json + ')');
            var data = obj["data"]['results'];
            var total = obj["data"]['size'];
            var endLength = total%pageSize;
            var eLength;
            if(pageNum == Math.ceil(total/pageSize)){
                if(endLength!==0){
                    eLength = endLength;
                }
                else{
                    eLength = dataList.pageSize;
                }
            }
            else {
                eLength = dataList.pageSize;
            }

            $('#dataTab').datagrid({
                title: '通知公告',
                fitColumns: true,
                showFooter: true,
                fit: true,
                width:iWidth,
                height:iHeight,
                striped:true,
                pagination:true,
                rownumbers:true,
                singleSelect:true,
                pageSize: pageSize,
                pageNumber: pageNum,
                columns: [getColumns],
                data:data,
                toolbar:[
                    {
                        //添加搜索---------------------------------------------------------------------------
                        id:'search'
                    }
                ],
                onClickRow:function (index,row) {
                    if(permit) {
                        for(var i=0;i<limit.length;i++){
                            if(limit[i]==row.bid){
                                return
                            }
                        }
                        limit.push(row.bid);
                        $_see(row.cid,row.bid,row.title,row.type,row.mtime,row.resources,row.editor)
                        adopt = true
                    }
                },
                onClickCell:function (index, field, value) {

                    if(field != "operate"){
                        permit = true;
                    }

                },
                onLoadSuccess:function() {

                    $('#search').textbox({
                        buttonText:'搜索',
                        iconAlign:'left',
                        width:200,
                        height:30,
                        onClickButton:function () {
                            if($('#search').textbox("getValue").replace(/(^\s*)|(\s*$)/g, "")==""){
                                $.messager.alert('确认','搜索不能为空');
                                return
                            }
                            searchValue = $('#search').textbox("getValue");
                            $_setDatagrid(undefined,undefined,$('#search').textbox("getValue"));
                        }
                    })
                }
            })
            var pager = $("#dataTab").datagrid("getPager");
            pager.pagination({
                total:total,
                pageList: [20,30,50,100,200,500,1000],
                pageSize:dataList.pageSize,
                pageNumber:dataList.page,
                beforePageText: '第',
                afterPageText: '页    共 ' + Math.ceil(total / dataList.pageSize) + ' 页',
                displayMsg: '当前显示 ' + (start + 1) + '-' + (start + eLength) + ' 条记录 共 ' + total + ' 条记录',
                onRefresh:function(pageNumber,pageSize){
                    searchValue = undefined;
                    $_setDatagrid();
                    // $_setDatagrid(pageSize,pageNumber)
                },
                onSelectPage:function (pageNo, pageSize) {
                    if(searchValue==''||searchValue==undefined){
                        $_setDatagrid(pageSize,pageNo);
                    }
                    else {
                        $_setDatagrid(pageSize,pageNo,searchValue);
                        console.info(searchValue);
                    }
                    // $_setDatagrid(pageSize,pageNo)
                }
            });
        })

    }

    function $_see(id,bid,title,type,mtime,resources,editor) {

        var mWidth = document.documentElement.offsetWidth
        var mHeight = document.documentElement.offsetHeight

        var dataCid = {"cid":id}

        $.get(urlInfo,dataCid,function(json) {

            var obj = eval('(' + json + ')');

            var data = obj["data"]
            var result = {};

            if (id == obj["data"].id) {
                obj["data"].title = title;
                obj["data"].type = type;
                obj["data"].mtime = mtime;
                obj["data"].resources = resources;
                obj["data"].editor = editor;
                result = obj["data"]
            }

            console.info(result)

            if (Permission) {

                $('#seeTabs').tabs({
                    border: false,
                    scrollDuration: 1,

                    onAdd:function () {



                        var readCid = {bid:bid}

                        $.get(urlRead,readCid,function(json) {

                        })
                    },
                    onClose: function (title, index) {

                        var tab = $('#seeTabs').tabs('tabs');
                        var len = tab.length;
                        limit.splice(index,1);
                        
                        if (len == 0) {
                            $('#seeView').dialog('close');
                        }

                    },
                })




                $('#seeView').dialog({
                    width: mWidth * 0.8,
                    height: mHeight-60,
                    title: "查看",
                    onOpen: function () {
                        $("a[name='opera']").linkbutton('disable');
                    },
                    onBeforeClose: function () {
                        //获取tabs的选项卡个数---------------------------------------------------------------------------
                        var tab = $('#seeTabs').tabs('tabs');
                        var len = tab.length;
                        //关闭所有选项---------------------------------------------------------------------------
                        for (var i = tab.length; i >= 0; i--) {
                            $("#seeTabs").tabs('close', i)
                        }
                        Permission = true;
                        $("a[name='opera']").linkbutton('enable');//关闭时启用删除按钮---------------------------------------------------------------------------
                        limit.length = 0;
                    }
                })


                Permission = false
            }

            var myDate = new Date(mtime);
            var time=myDate.getFullYear()+"年"+(myDate.getMonth()+1)+"月"+myDate.getDate()+'日'

            $('#seeTabs') .tabs('add', {
                title:result.title,
                content:"<h4 style='text-align: center; font-size: 25px; letter-spacing:1px; width: 60%; margin: 30px auto; padding-left:10px; padding-right:10px; word-wrap: break-word; word-break: normal; '>" + title + "</h4>" +

                "<div style=' margin: 0 auto; width: 75%;  padding: 10px;  font-size: 20px; text-indent:2em; min-height: 400px; letter-spacing:1px; word-wrap: break-word; word-break: normal;'>"

                + result.info +

                "</div>" +

                "<div style='width: 70%; margin: 0 auto;padding: 10px;'>"

                + "<span style='display: block;  line-height: 20px;  font-size: 20px; text-align: right; letter-spacing:1px;'>" + resources + ":" + editor + "</span>" +

                "<span style='display: block;  line-height: 20px;  font-size: 20px; text-align: right; margin-top: 10px; letter-spacing:1px; '>" + time + "</span>" +

                "</div>",
                closable: true,
                plain: true,
            })

        })
    }
})()